<template>
	<view>
		<view class="search-box">

			<uni-search-bar @input="input" radius="10" placeholder="自定义searchIcon"
				cancelButton="always"></uni-search-bar>
			<!-- 
			<view class="goods_item" v-show="searchResults===[]" v-for="(item,index) in totalListResult" v-key="index">
				<view class="goods_name" @click="gotoDetail(item)"> {{item.properties_name}} </view>
			</view> -->


			<!-- 	---- -->
			<view class="goods_item" v-show="searchResults.length!== 0" v-for="(item,index) in searchResults"
				v-key="index">
				<view class="goods_name" @click="gotoDetail(item)"> {{item.properties_name}} </view>
				<view class="">
					>
				</view>
			</view>

			<!-- 历史记录 -->
			<view class="history_box" v-show="searchResults.length === 0">
				<view class="">
					<text class="history_title">历史记录： </text>
					<uni-icons type="trash" size="20" @click="clearHistory"></uni-icons>
				</view>
			
				<view class="history_list"  v-for="(item,index) in historys"
					v-key="index">
					<uni-tag  class="history_item" @click="clickHistoryItem(item)" :text="item"></uni-tag>
	
				</view>
			</view>


		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				kw: '',
				timer: null,
				totalListResult: [],
				searchResults: [],
				historyList: ['a', '30']
			};
		},
		onLoad() {
			uni.request({
				url: 'http://localhost:3000/json_data',
				// url: '/api/json_data',
				success: (res) => {
					console.log(res.data.data);
					this.totalListResult = res.data.data
				},
				fail: (err) => {
					console.log(err);
				}

			})
		},
		computed: {
			historys() {
				return [...new Set(this.historyList)]
			}
		},



		methods: {
			// 清空搜索历史
			clearHistory(){
					this.historyList = []
			},
			gotoDetail(item) {
				uni.navigateTo({
					url: '../goods_detail/goods_detail?goods_id=' + item.sku_id
				})
			},

			//点击历史记录
			clickHistoryItem(item) {
				// this.input(item)
				uni.navigateTo({
					url:'../goods_list/goods_list?query='+item
				})
			},
			//input输入方法
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					if (this.kw == '') {
						this.searchResults = []
						return
					}
					//关键词搜索
					this.getSearch()
					const set = new Set(this.historyList)
					set.delete(this.kw)
					set.add(this.kw)
					this.historyList = [...set].reverse()
					// this.historyList.push(e)
				}, 500)
			},

			async getSearch() {
				this.searchResults = []
				if (this.kw === 0) {
					this.searchResults = []
					return
				}


				// console.log(this.kw);
				this.totalListResult.forEach((item, i) => {
					if (item.properties_name.indexOf(this.kw) != -1) {
						// console.log(i);
						this.searchResults.push(item)
					}
				})
				if (this.searchResults === []) {
					uni.showMsg()
					console.log('没有');
				}

			}
		}
	}
</script>

<style lang="scss">
	.goods_item {
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.goods_name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
	.history_box{
		display: flex;
		flex-direction: column;
		.history_list {
			width: 30px;
			display: flex;
			flex-direction: row;
		
			.history_item {
				margin: 5px;
				padding: 5px;
				// background-color: gainsboro;
				// border: 1px solid powderblue;
				border-radius: 15%;
				color: white
			}
		}
	}


</style>